let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({
  data: {
    customKeyboard: null,
    customCarNumInput: null,
    customCarNumInputIndex: 0,
    switch: false,
    inpVal: '',
    plate_desc: [],
    numbers: [{
      value: '',
      focus: true
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false
    }, {
      value: '',
      focus: false,
      eco: true
    }]
  },

  onLoad() {
    const that = this;
    that.getInit()
  },
  onSwitch() {
    const that = this;
    that.setData({
      switch: !that.data.switch,
      inpVal: '',
    })
    this.$selectComponent('#customKeyboard').hide();
    if (!that.data.switch) {
      that.onReady()

    }
  },
  onReady() {
    this.setData({
      customCarNumInputIndex: 0,
      numbers: [{
        value: '',
        focus: true
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false
      }, {
        value: '',
        focus: false,
        eco: true
      }]

    });
    this.changeKerboard(1);
  },
  getInit() {
    const that = this;
    myRequest.EasyHttp.post(api.reqUrls.getTips).then(res => {
      that.setData({
        plate_desc: res.data.result.plate_desc
      })
    })
  },
  changeKerboard(type) {
    this.$selectComponent('#customKeyboard').change(type);
    this.$selectComponent('#customKeyboard').show();
  },

  onInputFocus(e) {
    const index = e;
    if (index == 0) {
      this.changeKerboard(1);
    } else if (index == 7) {
      this.changeKerboard(3);
    } else {
      this.changeKerboard(2);
    }
    this.$selectComponent('#customKeyboard').show();
    this.setData({
      customCarNumInputIndex: index,
    });
  },

  onBtnClick(e) {

    this.$selectComponent('#customCarNumInput').change({
      index: this.data.customCarNumInputIndex,
      value: e,
      remove: false
    })
  },
  onChange(index) {
    if (index == 0) {
      this.changeKerboard(1);
    } else if (index == 7) {
      this.changeKerboard(3);
    } else {
      this.changeKerboard(2);
    }
    this.setData({
      customCarNumInputIndex: index,
    });
  },
  onBtnDdelete(e) {
    this.$selectComponent('#customCarNumInput').change({
      index: this.data.customCarNumInputIndex,
      value: '',
      remove: true
    })
  },
  onInp(e) {
    const that = this;
    that.setData({
      inpVal: e.detail.value
    })
  },
  showCarNo(e) {
    const that = this;
    const cardnumber = my.getStorageSync({
      key: 'cardnumber'
    }).data;
    if (!that.data.switch) {
      let numbers = this.$selectComponent('#customCarNumInput').props.numbers.map(item => item.value).join('')
      if (numbers) {
        let data = {
          cardnumber: cardnumber,
          platenumber: numbers
        }
        my.showLoading({
          title: '加载中...',
        })
        myRequest.EasyHttp.post(api.reqUrls.addLicensePlate, data).then(res => {
          my.hideLoading();
          my.stopPullDownRefresh();
          my.hideNavigationBarLoading();
          if (res.data.status == 1) {
            that.onReady()
            let pages = getCurrentPages(); //获取小程序页面栈
            let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
            beforePage.getData(); //触发上个页面自定义的shuaxin方法
            my.navigateBack({
              delta: 1
            })
          } else {
            my.showModal({
              content: res.data.msg,
              showCancel: false,
              confirmText: '我知道了',
              confirmColor: '#ff5151',
            })

          }

        })
      } else {
        my.showToast({
          content: '请选择车牌号',
          type: 'exception',
        })
      }
    } else {
      if (that.data.inpVal) {
        let data = {
          cardnumber: cardnumber,
          platenumber: that.data.inpVal
        }
        my.showLoading({
          title: '加载中...',
        })
        myRequest.EasyHttp.post(api.reqUrls.addLicensePlate, data).then(res => {
          my.hideLoading();
          my.stopPullDownRefresh();
          my.hideNavigationBarLoading();
          if (res.data.status == 1) {
            let pages = getCurrentPages(); //获取小程序页面栈
            let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
            beforePage.getData(); //触发上个页面自定义的shuaxin方法
            my.navigateBack({
              delta: 1
            })
          } else {
            my.showModal({
              content: res.data.msg,
              showCancel: false,
              confirmText: '我知道了',
              confirmColor: '#ff5151',
            })

          }

        })
      } else {
        my.showToast({
          content: '请填写车牌号',
          type: 'exception',
        })

      }
    }

  }
})